package org.wsff.tools.api.gateway.error;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;

/**
 * 断言
 *
 * @author ryan
 * @version Id: Assert.java, v 0.1 2022-01-18 17:18 ryan Exp $$
 */
public final class Assert {

    /**
     * 参数不为真
     *
     * @param value 入参
     * @param message 校验异常
     * @throws ValidationException exp
     */
    public static void notTrueValidate(boolean value, String message) throws ValidationException {
        if (!value) {
            throw new ValidationException(AbstractErrorMessageFactory.createStaticError(message));
        }
    }

    /**
     * 参数不为空
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    public static void notNullValidate(Object value, String message) throws ValidationException {
        if (value == null) {
            throw new ValidationException(AbstractErrorMessageFactory.createStaticError(message));
        }
    }

    /**
     * 非法参数
     *
     * @param value 入参
     * @param message 参数名
     * @throws ValidationException 校验异常
     */
    public static void invalidValidate(Object value, String message) throws ValidationException {
        if (value == null) {
            throw new ValidationException(AbstractErrorMessageFactory.createStaticError(message));
        }
    }

    /**
     * 字符串不为空或空字符串
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    public static void notStringEmptyValidate(String value, String message) throws ValidationException {
        if (StringUtils.isBlank(value)) {
            throw new ValidationException(AbstractErrorMessageFactory.createStaticError(message));
        }
    }

    /**
     * 集合不为空或空字符串
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    public static void notCollectionEmptyValidate(Collection<?> value, String message) throws ValidationException {
        if (value == null || value.isEmpty()) {
            throw new ValidationException(AbstractErrorMessageFactory.createStaticError(message));
        }
    }
}
